﻿using FJASPX.Framwork.Constant;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;

namespace FJASPX.Framwork.Helper
{
    /// <summary>
    /// 后缀为HTML的，都经这里处理
    /// web.config
    /// <add verb="*" path="*.html" type="DCommon.DHandler"/>
    /// </summary>
    public class DHandlerHTML : IHttpHandler
    {
        public bool IsReusable
        {
            get { return false; }
        }

        /// <summary>
        /// 获取物理路径，判断文件夹中有没有存在这个文件
        /// 不存在的话，就会调用FilterStream类进行创建,并写入内容
        /// 存在的话，就直接显示页面
        /// </summary>
        public void ProcessRequest(HttpContext context)
        {
            HttpRequest request = context.Request;
            HttpResponse response = context.Response;

            string htmlPage = request.Url.AbsolutePath;
            string file = context.Server.MapPath(htmlPage);
            if (!File.Exists(file))
            {
                string aspxPage = StringHelper.HtmlToAspx(htmlPage);
                response.Redirect(aspxPage);
                return;
            }
            response.WriteFile(file);
            return;
        }
    }

    /// <summary>
    /// 后缀为HTML的，都经这里处理
    /// web.config
    /// <add verb="*" path="*.html" type="DCommon.DHandler"/>
    /// </summary>
    public class DHandlerIMG : IHttpHandler
    {
        public bool IsReusable
        {
            get { return false; }
        }

        /// <summary>
        /// 获取物理路径，判断文件夹中有没有存在这个文件
        /// 不存在的话，就会调用FilterStream类进行创建,并写入内容
        /// 存在的话，就直接显示页面
        /// </summary>
        public void ProcessRequest(HttpContext context)
        {
            HttpRequest request = context.Request;
            HttpResponse response = context.Response;

            string htmlPage = request.Url.AbsolutePath;
            string file = context.Server.MapPath(htmlPage);
            if (!File.Exists(file))
            {
                response.Redirect(Cfg.NO_IMG);
                return;
            }
            response.WriteFile(file);
            return;
        }
    }
}
